package com.xnyzc.lhy.order.mq;

import com.alibaba.fastjson.JSON;
import com.xnyzc.lhy.common.component.config.security.RocketConfig;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.constant.MqConstant;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.order.entity.param.order.OrderParam;
import com.xnyzc.lhy.order.service.sidebar.IOaCNearbyDriverPositionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: huxuekuo
 * @Date: 2019-07-18 14:50
 * @Description:
 */
@Slf4j
@Component
public class MqOrderProducer {
    /**
     * 生产者
     */
    @Autowired
    private DefaultMQProducer defaultMQProducer;
    /**
     * rocket配置类
     */
    @Autowired
    RocketConfig rocketConfig;

    @Autowired
    RedisCommon redisCommon;

    @Autowired
    IOaCNearbyDriverPositionService cNearbyDriverPositionService;

    /**
     * 派发订单,队列中大于 1 代表处理本身还有其他人需要等待
     *
     * @param order 订单对象
     * @return
     */
    public Rv sendMsg(OrderParam order) {
        Message message = new Message(rocketConfig.getTopic(), MqConstant.ORDER_DISPATCH, JSON.toJSONString(order).getBytes());
        try {
            SendResult send = defaultMQProducer.send(message);
            log.info("订单信息{},发送结果{}", order, send);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 给滴滴同步(做数据同步的发送)
     */
    public void sendMsg(Object param, String tags) {
        sendMsg(param, tags, null);
    }

    /**
     * 给滴滴同步(做数据同步的发送)
     */
    public void sendMsg(Object param, String tags, Integer delayLevel) {
        log.info("进入发送消息方法(Start)");
        Message message = new Message(rocketConfig.getTopic(), tags, JSON.toJSONString(param).getBytes());
        if (delayLevel != null && delayLevel > 0) {
            message.setDelayTimeLevel(delayLevel);
        }
        try {
            SendResult send = defaultMQProducer.send(message);
            log.info("MQ发送消息 订单信息{},发送结果{}", param, send);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        log.info("发送消息结束(End)");
    }
}
